/*
 *    Copyright (c) 2018-2025, nart All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: nart
 */
package com.nai365.nart.member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nai365.nart.member.dto.LatestVersion;
import com.nai365.nart.member.entity.App;
import com.nai365.nart.member.mapper.AppMapper;
import com.nai365.nart.member.service.AppService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * app管理
 *
 * @author baogs
 * @date 2022-07-07 11:36:55
 */
@Service
public class AppServiceImpl extends ServiceImpl<AppMapper, App> implements AppService {

	@Override
	public LatestVersion latest(String os) {
		LatestVersion latestVersion = LatestVersion.builder()
				.latestVersion("")
				.latestUpdateText("")
				.latestForceVersion("")
				.build();
		LambdaQueryWrapper<App> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(App::getOs, os)
				.eq(App::getIsOnline, true)
				.orderByDesc(App::getUpdateTime);
		List<App> latestList = this.list(queryWrapper);
		if (latestList != null && !latestList.isEmpty()) {
			App latest = latestList.get(0);
			latestVersion.setLatestVersion(latest.getVersionName());
			latestVersion.setLatestUpdateText(latest.getUpdateText());
		}
		queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(App::getOs, os)
				.eq(App::getIsForceUpdate, true)
				.eq(App::getIsOnline,true)
				.orderByDesc(App::getUpdateTime);
		List<App> forceList = this.list(queryWrapper);
		if (forceList != null && !forceList.isEmpty()) {
			App force = forceList.get(0);
			latestVersion.setLatestForceVersion(force.getVersionName());
		} else {
			if (latestList != null && !latestList.isEmpty()) {
				App latest = latestList.get(0);
				latestVersion.setLatestForceVersion(latest.getVersionName());
			}
		}
		return latestVersion;
	}
}
